package com.chap17.completablefuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * <code>Demo04</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-05-09 9:51
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
@Slf4j
public class Demo07 {
    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        log.debug("monkey进入餐厅，点了份西红柿炒番茄");
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            log.debug("厨师炒菜");
            sleep(2, TimeUnit.SECONDS);
            return "西红柿炒番茄好了";
        },executorService)
                //服务员还要蒸饭，饭好了，服务员打饭
                //thenCombine 第一个参数是另外一个任务，第二个参数是对两个任务合并进行处理
                .thenCombine(CompletableFuture.supplyAsync(()->{
                    log.debug("服务员蒸饭");
                    sleep(3,TimeUnit.SECONDS);
                    return "米饭好了";
                        }),
                        (dish,rice)->{
                    log.debug("服务员打饭");
                    sleep(1,TimeUnit.SECONDS);
                    return dish+" "+rice;
                        })
                ;


        log.debug("monkey在刷抖音");
        log.debug("{},monkey开吃",cf.join());
    }

    private static void sleep(int i, TimeUnit seconds) {

        try {
            seconds.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
